﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Moose.Common
{
    public class IniHelper
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);


        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// Ini文件地址
        /// </summary>
        private string IniFilePath;

        public IniHelper(string iniFilePath)
        {
            this.IniFilePath = iniFilePath;
        }

        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="section">项目名称(如 [TypeName] )</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public void WriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, this.IniFilePath);
        }

        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="section">项目名称(如 [TypeName] )</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <param name="size">大小</param>
        /// <returns>string类型的返回值</returns>
        public string ReadStringValue(string section, string key, string def, int size)
        {
            string retValue = string.Empty;
            try
            {
                StringBuilder temp = new StringBuilder(size);
                int i = GetPrivateProfileString(section, key, def, temp, size, this.IniFilePath);
                retValue = temp.ToString();
            }
            catch
            {
                retValue = string.Empty;
            }
            return retValue;
        }

        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="section">项目名称(如 [TypeName] )</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <param name="size">大小</param>
        /// <returns>double类型的返回值</returns>
        public double ReadDoubleValue(string section, string key, double def, int size)
        {
            double retValue = 0;
            try
            {
                StringBuilder temp = new StringBuilder(size);
                int i = GetPrivateProfileString(section, key, def.ToString(), temp, size, this.IniFilePath);
                retValue = Convert.ToDouble(temp.ToString());
            }
            catch
            {
                retValue = 0;
            }
            return retValue;
        }

        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="section">项目名称(如 [TypeName] )</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <param name="size">大小</param>
        /// <returns>float类型的返回值</returns>
        public float ReadFloatValue(string section, string key, float def, int size)
        {
            float retValue = 0;
            try
            {
                StringBuilder temp = new StringBuilder(size);
                int i = GetPrivateProfileString(section, key, def.ToString(), temp, size, this.IniFilePath);
                retValue = float.Parse(temp.ToString());
            }
            catch
            {
                retValue = 0;
            }
            return retValue;
        }

        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="section">项目名称(如 [TypeName] )</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <param name="size">大小</param>
        /// <returns>decimal类型的返回值</returns>
        public decimal ReadDecimalValue(string section, string key, decimal def, int size)
        {
            decimal retValue = 0;
            try
            {
                StringBuilder temp = new StringBuilder(size);
                int i = GetPrivateProfileString(section, key, def.ToString(), temp, size, this.IniFilePath);
                retValue = Convert.ToDecimal(temp.ToString());
            }
            catch
            {
                retValue = 0;
            }
            return retValue;
        }

        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="section">项目名称(如 [TypeName] )</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <param name="size">大小</param>
        /// <returns>int类型的返回值</returns>
        public int ReadIntValue(string section, string key, int def, int size)
        {
            int retValue = 0;
            try
            {
                StringBuilder temp = new StringBuilder(size);
                int i = GetPrivateProfileString(section, key, def.ToString(), temp, size, this.IniFilePath);
                retValue = Convert.ToInt32(temp.ToString());
            }
            catch
            {
                retValue = 0;
            }
            return retValue;
        }

        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="section">项目名称(如 [TypeName] )</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <param name="size">大小</param>
        /// <returns>decimal类型的返回值</returns>
        public bool ReadBoolValue(string section, string key, bool def)
        {
            bool retValue = false;
            try
            {
                StringBuilder temp = new StringBuilder(125);
                int i = GetPrivateProfileString(section, key, def.ToString(), temp, 125, this.IniFilePath);
                if (temp.ToString() == "1")
                    retValue = true;
                else
                    retValue = false;
            }
            catch
            {
                retValue = false;
            }
            return retValue;
        }

        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="section">项目名称(如 [TypeName] )</param>
        /// <param name="key">键</param>
        /// <param name="def">默认值</param>
        /// <param name="size">大小</param>
        /// <returns>float类型的返回值</returns>
        public byte ReadByteValue(string section, string key, string def, int size)
        {
            byte retValue = Convert.ToByte(0);
            try
            {
                StringBuilder temp = new StringBuilder(size);
                int i = GetPrivateProfileString(section, key, def, temp, size, this.IniFilePath);
                retValue = Convert.ToByte(temp.ToString());
            }
            catch
            {
                retValue = Convert.ToByte(0);
            }
            return retValue;
        }


        public void ClearAll(string section)
        {
            WritePrivateProfileString(null, null, null, this.IniFilePath);
        }

        public void ClearSection(string section)
        {
            WritePrivateProfileString(section, null, null, this.IniFilePath);
        }

        public void ClearKeyValue(string section, string key)
        {
            WritePrivateProfileString(section, key, null, this.IniFilePath);
        }


        /// <summary>
        /// 验证文件是否存在
        /// </summary>
        /// <returns>布尔值</returns>
        public bool Exists()
        {
            return File.Exists(IniFilePath);
        }

    }
}
